Omplus.proxy.Proxy = Omplus.base.extend(Omplus.base.Base, {
    omplusType:"Proxy",
    requestModel:null,
    responseModel:null,
    successCallback:null,
    errorCallback:null,
    badRequestCallback:null,
    callback:function () {
    },
    start:function () {
    },

    getResult:function () {
    },

    setResult:function (model) {
    },

    convertErrors:function (errors) {
        var array = new Array();
        for (var i in errors) {
            array.push({value:errors[i]})
        }
        return array;
    }
});

Omplus.proxy.AjaxProxy = Omplus.base.extend(Omplus.proxy.Proxy, {
    omplusType:"AjaxProxy",

    runProxy:function (actionName, requestModel, responseModel, callback) {
        this.start();
        this.requestModel = requestModel;
        this.responseModel = responseModel;
        this.callback = callback;
        var currentProxy = this;
        if (Omplus.globals.getObject(requestModel).isValid()) {
            $.ajax({
                type:"get",
                url:"action.php?function=" + actionName + "&parameters=" + encodeURIComponent(Omplus.globals.getObject(requestModel).toJSON()),
                data:{},
                success:function (data) {
                   //try {
                        Omplus.globals.getObject(currentProxy.responseModel).setStatus(data[Omplus.constants.status]);
                        switch (data[Omplus.constants.status]) {
                            case Omplus.constants.status_ok :
                                Omplus.globals.getObject(currentProxy.responseModel).errors.empty();
                                Omplus.globals.getObject(currentProxy.responseModel).setValue(data[Omplus.constants.response_parameters], null);
                                break;
                            case Omplus.constants.status_error :
                                if (data[Omplus.constants.response_parameters]) {
                                    if (data[Omplus.constants.response_parameters][Omplus.constants.response_errors]) {
                                        Omplus.globals.getObject(currentProxy.responseModel).setErrors(currentProxy.convertErrors(data[Omplus.constants.response_parameters][Omplus.constants.response_errors]));
                                    }
                                }
                                if (data[Omplus.constants.response_errors]) {
                                    Omplus.globals.getObject(currentProxy.responseModel).setErrors(currentProxy.convertErrors(data[Omplus.constants.response_errors]));
                                }
                                break;
                            default:
                                Omplus.globals.getObject(currentProxy.responseModel).setStatus(Omplus.constants.status_bad_request);
                                break;
                        }
                    /*} catch (e) {
                        Omplus.globals.getObject(currentProxy.responseModel).setStatus(Omplus.constants.status_bad_request);
                    }*/
                    var view = Omplus.globals.getStatusesReferences(currentProxy.responseModel);
                    if (view){
                        Omplus.globals.getObject(view).setValue({}, currentProxy.responseModel);
                    }
                    currentProxy.callback();
                },
                error:function (data) {
                    Omplus.globals.getObject(currentProxy.responseModel).setStatus(Omplus.constants.status_bad_request);
                    currentProxy.callback();
                },
                dataType:"json"
            });
        } else {
            Omplus.globals.getObject(requestModel).showErrors();
            callback();
        }
    },

    getResult:function (actionName, requestModel, responseModel, callback) {
        this.runProxy(actionName, requestModel, responseModel, callback);
    },

    setResult:function (actionName, requestModel, responseModel, callback) {
        this.runProxy(actionName, requestModel, responseModel, callback);
    }
});

Omplus.proxy.LocalStorage = Omplus.base.extend(Omplus.proxy.Proxy, {
    omplusType:"LocalStorage",

    runProxy:function (actionName, requestModel, responseModel, callback) {
        this.start();
        this.requestModel = requestModel;
        this.responseModel = responseModel;
        this.callback = callback;
        this.function = "function=" + this.key + "&parameters=" + JSON.stringify(Omplus.globals.getObject(this.requestModel).getValue());
    },

    getResult:function (actionName, requestModel, responseModel, callback) {
        this.runProxy(actionName, requestModel, responseModel, callback);
        var result = localStorage.getItem(this.function);
        if (result != null)
            Omplus.globals.getObject(this.responseModel).setValue(JSON.parse(result), null);
        this.callback();
    },

    setResult:function (actionName, requestModel, responseModel, callback) {
        this.runProxy(actionName, requestModel, responseModel, callback);
        localStorage.setItem(this.function, JSON.stringify(Omplus.globals.getObject(this.responseModel).getValue()));
        this.callback();
    }
});